package com.renjianbo.project.module.system.controller;

import com.renjianbo.project.common.model.PageInfo;
import com.renjianbo.project.common.model.PageReq;
import com.renjianbo.project.common.util.JsonUtil;
import com.renjianbo.project.module.system.model.sys_permission.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.renjianbo.project.module.system.service.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import com.renjianbo.project.common.model.Rest;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2022-04-19
 */
@Api(tags = {""})
@RestController
@RequestMapping("sys-permission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;


    /**
     * 同步权限
     */
    @PostMapping(value = "sync-permission")
    @ResponseBody
    public Rest<String> syncPermission(@RequestBody List<SysPermissionSyncReq> permissionSyncFormList) {
        sysPermissionService.syncPermission(permissionSyncFormList);

        return Rest.success();
    }

    @ApiOperation(value = "查询数据")
    @GetMapping("list")
    public Rest<List<SysPermissionVO>> list(SysPermissionSearchReq req){
        List<SysPermissionDTO> list = sysPermissionService.list(req);
        List<SysPermissionVO> voList = JsonUtil.listTransform(list, SysPermissionVO.class);

        return Rest.success(voList);
    }

    @ApiOperation(value = "根据id查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="-id",required=true),
    })
    @GetMapping("get-by-id")
    public Rest<SysPermissionVO> getById(@RequestParam String id){
        SysPermissionDTO sysPermissionDTO = sysPermissionService.getById(id);
        SysPermissionVO sysPermissionVO = new SysPermissionVO();
        BeanUtils.copyProperties(sysPermissionDTO, sysPermissionVO);

        return Rest.success(sysPermissionVO);
    }

    @ApiOperation(value = "分配权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "distributionToken", value = "请求头token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "角色id", required = true),
            @ApiImplicitParam(paramType = "query", name = "permissionIds", value = "权限id数组", required = true)
    })
    @RequestMapping(value = "assigns-permission")
    @ResponseBody
    public Rest<String> assignsPermission(@NotNull Long roleId, @NotNull Long[] permissionIds){
        sysPermissionService.assignsPermission(roleId, permissionIds);

        return Rest.success();
    };

}
